package com.iflysse.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author My
 * @date 2021/10/19 14:36
 */
public class JwtUtil {
    private static final Integer date = 365;
    private static  final String key = "iflytek";
    public static String createToken(String userId, String role) throws Exception {

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE,date);
        Date date = c.getTime();

        Map<String,Object> map =new HashMap<String,Object>();
        map.put("alg","HS256");
        map.put("type","JWT");

        String token = JWT.create().withHeader(map)
                .withClaim("userId",userId)
                .withClaim("role",role)
                .withSubject("生成token")
                .withIssuedAt(c.getTime())
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(key));
        return  token;
    }
}
